package com.liangxy.news;

import android.app.Application;
import android.util.Log;

import com.liangxy.news.api.ApiClient;
import com.liangxy.news.database.AppDatabase;

/**
 * 应用程序入口点，用于初始化全局组件
 */
public class NewsApplication extends Application {

    private static final String TAG = "NewsApplication";
    
    @Override
    public void onCreate() {
        super.onCreate();
        
        // 初始化数据库
        initializeDatabase();
        
        // 初始化ApiClient
        initializeApiClient();
    }
    
    @Override
    public void onTerminate() {
        super.onTerminate();
        
        // 关闭数据库连接
        AppDatabase.closeDatabase();
        Log.d(TAG, "Database connection closed in Application.onTerminate()");
    }
    
    /**
     * 初始化数据库
     */
    private void initializeDatabase() {
        try {
            AppDatabase.getInstance(getApplicationContext());
            Log.d(TAG, "Database initialized successfully in Application");
        } catch (Exception e) {
            Log.e(TAG, "Error initializing database: " + e.getMessage(), e);
        }
    }
    
    /**
     * 初始化ApiClient
     */
    private void initializeApiClient() {
        try {
            ApiClient.init(getApplicationContext());
            Log.d(TAG, "ApiClient initialized with key: " + ApiClient.getApiKey());
        } catch (Exception e) {
            Log.e(TAG, "Error initializing ApiClient: " + e.getMessage(), e);
        }
    }
} 